﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Web;

namespace DarrenSoft.Core
{
    public class PageRequest
    {
        /// <summary>
        /// 从地址栏获取值
        /// </summary>
        /// <param name="page">页面</param>
        /// <param name="para">参数</param>
        /// <returns></returns>
        public static string GetQueryString(string para)
        {
            string queryString = "";
            if (System.Web.HttpContext.Current.Request.QueryString[para] != null)
            {
                queryString = System.Web.HttpContext.Current.Request.QueryString[para].ToString();
            }
            else
            {
                queryString = "";
            }
            return StringHelper.InputTexts(queryString.Trim());
        }

        /// <summary>
        /// 从地址栏获取相关int值
        /// </summary>
        /// <param name="page">页面</param>
        /// <param name="para">参数</param>
        /// <returns></returns>
        public static int GetQueryInt(string para)
        {
            int queryInt = -1;
            string tempQueryString = GetQueryString(para);
            if (ValidateHelper.IsNumber(tempQueryString))
            {
                queryInt = int.Parse(tempQueryString);
            }
            return queryInt;
        }

        /// <summary>
        /// 从表单项获取相关值
        /// </summary>
        /// <param name="page">页面</param>
        /// <param name="para">参数</param>
        /// <returns></returns>
        public static string GetFormString(string para)
        {
            string formString = "";
            if (System.Web.HttpContext.Current.Request.Form[para] != null)
            {
                formString = System.Web.HttpContext.Current.Request.Form[para].ToString();
            }
            else
            {
                formString = "";
            }
            return StringHelper.InputTexts(formString.Trim());
        }

        /// <summary>
        /// 从表单获取Int类型的值
        /// </summary>
        /// <param name="page">页面</param>
        /// <param name="para">参数</param>
        /// <returns></returns>
        public static int GetFormInt(string para)
        {
            int formInt = -1;
            string tempFormString = GetFormString(para);
            if (ValidateHelper.IsNumber(tempFormString))
            {
                formInt = int.Parse(tempFormString);
            }
            return formInt;
        }

        /// <summary>
        /// 从页面中获取string类型值
        /// </summary>
        /// <param name="page">页面</param>
        /// <param name="para">参数</param>
        /// <returns></returns>
        public static string GetString(string para)
        {
            if (System.Web.HttpContext.Current.Request.RequestType.ToLower().Equals("get"))
            {
                return GetQueryString(para);
            }
            else
            {
                return GetFormString(para);
            }
        }

        /// <summary>
        /// 从页面中获取Int类型的值
        /// </summary>
        /// <param name="page">页面</param>
        /// <param name="para">参数</param>
        /// <returns></returns>
        public static int GetInt(string para)
        {
            string tempInt = GetString(para);
            int intReturn = -1;
            if (ValidateHelper.IsNumber(tempInt))
            {
                intReturn = int.Parse(tempInt);
            }
            return intReturn;
        }

        /// <summary>
        /// 从参数中获取相关参数数组
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public static Hashtable GetQueryPara()
        {
            int paraCount = System.Web.HttpContext.Current.Request.QueryString.Count;
            string queryKey = "";
            string queryValue = "";
            Hashtable para = new Hashtable();
            for (int i = 0; i < paraCount; i++)
            {
                queryKey = System.Web.HttpContext.Current.Request.QueryString.Keys[i].ToString();
                queryValue = System.Web.HttpContext.Current.Request.QueryString[i].ToString();
                if ((queryKey.IndexOf("w_") >= 0 || queryKey.IndexOf("q_") >= 0) && !queryValue.Equals(""))
                {
                    para.Add(queryKey, queryValue);
                }
            }
            return para;

        }


        /// <summary>
        /// 获取客户端IP
        /// </summary>
        /// <returns></returns>
        public static string GetIP()
        {
            // 优先取得代理IP 
            string userHostAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (string.IsNullOrEmpty(userHostAddress))
            {
                //没有代理IP则直接取客户端IP 
                userHostAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
            if (!string.IsNullOrEmpty(userHostAddress))
            {
                return userHostAddress;
            }
            return "0.0.0.0";
        }
        /// <summary>
        /// 从表单中获取相关参数数组
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public static Hashtable GetFormPara()
        {
            int paraCount = System.Web.HttpContext.Current.Request.Form.Count;
            string formKey = "";
            string formValue = "";
            Hashtable para = new Hashtable();
            for (int i = 0; i < paraCount; i++)
            {
                formKey = System.Web.HttpContext.Current.Request.Form.Keys[i].ToString();
                formValue = System.Web.HttpContext.Current.Request.Form[i].ToString();
                if ((formKey.IndexOf("w_") >= 0 || formKey.IndexOf("q_") >= 0) && !formValue.Equals(""))
                {
                    para.Add(formKey, formValue);
                }
            }

            return para;

        }

        /// <summary>
        /// 获取参数数组
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public static Hashtable GetPara()
        {
            if (System.Web.HttpContext.Current.Request.RequestType.ToLower().Equals("get"))
            {
                return GetQueryPara();
            }
            else
            {
                return GetFormPara();
            }
        }


    }
}
